package com.lli.mc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.lli.mc.converter.jave.AudioConverter;
import com.lli.mc.untils.FileUtil;

/**
 * 文件上传
 * 
 * @author lli
 *
 * @version 1.0
 *
 */
@Api(description = "多媒体转换服务")
@RestController
public class FileUploadController {

    /**
     * 
     * @description 音频转MP3(单文件上传 )
     * @author lli
     * @create 2017年7月28日下午1:15:13
     * @version 1.0
     * @param file
     * @return
     */
    @ApiOperation(value = "音频转MP3", notes = "单文件音频格式转换为mp3")
    @RequestMapping(value = "/toMp3", method = RequestMethod.POST)
    public void upload(@RequestParam("file") MultipartFile file,
            HttpServletResponse res) {
        if (!file.isEmpty()) {
            try {
                // 上传文件临时文件
                File tmp = File.createTempFile("test",
                        file.getOriginalFilename());
                // 转换后的临时文件
                File tmp_mp3 = File.createTempFile("test-mp3",
                        file.getOriginalFilename());
                // 转换上传的文件到本地临时文件
                file.transferTo(tmp);
                // 获取扩展名称
                String extName = FileUtil.getExtensionName(file
                        .getOriginalFilename());
                // 获取文件名称去除扩展名称
                String filename = file.getOriginalFilename().substring(
                        file.getOriginalFilename().indexOf(extName));
                // 转换MP3
                AudioConverter.changeToMp3(tmp, tmp_mp3);

                FileUtil.down(res, tmp_mp3, filename);

                tmp.delete();
                tmp_mp3.delete();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }

    /**
     * 
     * @description 多文件上传
     * @author lli
     * @create 2017年7月28日下午4:44:38
     * @version 1.0
     * @param request
     * @return
     */
    // @RequestMapping(value = "/upload/batch", method = RequestMethod.POST)
    public @ResponseBody String batchUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request)
                .getFiles("file");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(
                            new File(file.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();
                } catch (Exception e) {
                    stream = null;
                    return "You failed to upload " + i + " => "
                            + e.getMessage();
                }
            } else {
                return "You failed to upload " + i
                        + " because the file was empty.";
            }
        }
        return "upload successful";
    }

}